import SSHUtil from "./ssh/shhClient";
import {api} from "./http/http";
import server from "./http/http";

server();
api("/top",async function(req,res){
  
    let topkey  = "top"+new Date().getTime();
    SSHUtil.exec({
        cmd: "top -b",
        key: topkey,
        success:function(data,key){
           if(key == topkey){
              // console.log(data); 
               topWrite(data,res);
           }      
        }
    })
})

api("/pwd",async function(req,res){
  
    let topkey  = "pwd"+new Date().getTime();
    SSHUtil.exec({
        cmd: "pwd",
        key: topkey,
        success:function(data,key){
           if(key == topkey){
              // console.log(data); 
               topWrite(data,res);
           }      
        }
    })
})

const  patt1=new RegExp("PID USER");
function topWrite(data,res){  
    data = data.toString("utf-8",0,curIndex);
    //console.log(data);
    
    let keys = [];
    let rsList = [];
    let countStr = "";
    let index = 1;
    let curIndex = data.indexOf(10);
    const maxSize = data.length;  
    let isContent = false;
    while(curIndex!=-1&&curIndex<=maxSize){
        let line = data.substring(0,curIndex);
        if(index>=5){
            if(isContent){
                let values = line.trim().replace(/\s+/g,",").split(",");
                let item = {};
                for (let index = 0; index < values.length; index++) {
                    const element = values[index];                    
                    item[keys[index]] = element;
                }
                rsList.push(item);
            }else{
                if(patt1.test(line)){                 
                    keys = line.trim().replace(/\s+/g,",").split(",");
                    isContent=true;
                }else{
                    countStr += line+"\n";
                }
            }
        }else{
            countStr += line+"\n";
        }
        data = data.substring(curIndex+1,maxSize)
        curIndex = data.indexOf("\n");
        index++;
    }
    // res.writeHead(200, {     
    //     'Content-Type': 'application/json' });
    res.write(JSON.stringify({count:countStr,process:rsList}));
   // res.end();
}
async function app() {    
    let rs = await SSHUtil.init({
        host: '119.28.188.55',
        port: 22,
        username: 'root',
        password: 'Wyd1313123'
        //privateKey: require('fs').readFileSync('/home/admin/.ssh/id_dsa')
    })  
    let topkey  = "top"+new Date().getTime();
    let pwdkey  = "pwd"+new Date().getTime();
    SSHUtil.exec({
        cmd: "top -b",
        key: topkey,
        success:function(data,key){
         
           if(key == topkey){
            console.log(data+"");
                SSHUtil.exec({
                    cmd: "pwd",
                    key: pwdkey,
                    success:function(data,key){                        
                        
                        if(key == pwdkey){
                            console.log(data+""); 
                        }      
                    }
                })
              
           }      
        }
    })
    

   

    //server();
    
   
}


// function server(){

// }

app();


